package day05_api.com.itheima.d4_sf;

public class Demo3 {
    public static void main(String[] args) {
        int[] arr = {2, 6, 2, 4, 5, 1, 3, 7};
        int index = searchDataIndex(arr, 4);
        System.out.println("arr[" + index + "] = " + arr[index]);

        int index1 = searchDataIndex(arr, 8);
        System.out.println("arr[" + index1 + "] = " + arr[index1]);
    }
    public static int searchDataIndex(int[] array, int number){
        int left = 0;
        int right = array.length - 1;
        while (left <= right){
            int mid = (left + right) / 2;
            if (number < array[ mid]){
                right = mid - 1;
            }else if (number > array[mid]){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
